#ifndef USER_PLUGIN_DUMMY
#define USER_PLUGIN_DUMMY
#include <iostream>
#include <string>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include "../../../libpland/lib/plugin.hpp"
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <boost/thread.hpp>
#include <fftw3.h>
#include <cmath>

using namespace std;


void bubbleSort(char numbers[], int array_size)
{
  int i, j;
  char temp;
 
  for (i = (array_size - 1); i > 0; i--)
  {
    for (j = 1; j <= i; j++)
    {
      if (numbers[j-1] > numbers[j])
      {
        temp = numbers[j-1];
        numbers[j-1] = numbers[j];
        numbers[j] = temp;
      }
    }
  }
}

class CoreC_Sin : public UserC_Plugin
{
public:
    CoreC_Sin();
    ~CoreC_Sin();
    void start();
    void stop();
    void load_queues(CoreC_DQueue * front ,CoreC_DQueue * back);
private:
    CoreC_DQueue *_front;
    CoreC_DQueue *_back;
    bool _running;

};

#endif


CoreC_Sin::CoreC_Sin()
{
    _running = false;
}

void CoreC_Sin::start()
{
    _running = true;
    CoreC_Data * packet = NULL;


    while(_running)
    {

        CoreC_Data_Header *h;
        int n;
        packet = _front->pop();
        double *unaligned = (double*)packet->get_data();
        h = packet->get_header();
        n = h->_size/sizeof(double);
        for(int i = 0; i< n; i++)
        {
            unaligned[i] = sin(unaligned[i])*cos(unaligned[i])*1.025;
        }
        bubbleSort((char*)unaligned, h->_size);
        _back->push(packet);
    }
}

void CoreC_Sin::stop()
{
    _running = false;
}

void CoreC_Sin::load_queues(CoreC_DQueue* front ,CoreC_DQueue* back)
{
    _front  = front;
    _back  = back;
}


extern "C"
{
    UserC_Plugin* Core_GetInstance()
    {
        return new CoreC_Sin();
    }

    void Core_KillInstance(UserC_Plugin* a)
    {
        delete a;
    }
}
